package com.tanovo.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanovo.bo.*;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.User;
import com.tanovo.domain.UserBaseInfo;
import com.tanovo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags = "用户信息")
@Slf4j
public class UserApi {

    @Autowired
    private IUserService iUserService;

    @PostMapping("/user/add")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public ResultObject addNewUser(@RequestBody @Validated UserBaseInfoBo userBaseInfoBo) {
        userBaseInfoBo.setUpdateBy(UserInfoContext.getUser().getId().toString());
        userBaseInfoBo.setCreateBy(UserInfoContext.getUser().getId().toString());
        iUserService.addUserInfo(userBaseInfoBo);
        return new ResultObject();
    }

    @PostMapping("/user/restPassword")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    public ResultObject restPassword(@RequestBody @Validated UserRestBo userRestBo) {
        iUserService.restPassword(userRestBo);
        return new ResultObject();
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    public ResultObject update(@RequestBody @Validated UserBaseInfoBo user) {
        iUserService.updateUserBaseInfo(user);
        return new ResultObject();
    }

    @PostMapping("/user/delete/list")
    @ApiOperation(value = "批量删除用户", notes = "批量删除用户")
    public ResultObject deleteUser(@RequestBody List<Integer> userIdList) {
        iUserService.deleteByIdList(userIdList);
        return new ResultObject();
    }

    @GetMapping("/user/detail")
    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    public ResultObject userDetail(@RequestParam Integer userId) {
        UserBaseInfo user = iUserService.getUserDetail(userId);
        return new ResultObject(user);
    }

    @GetMapping("/user/detail/token")
    @ApiOperation(value = "token获取用户详情", notes = "token获取用户详情")
    public ResultObject userDetail() {
        Integer userid = UserInfoContext.getUser().getId();
        UserBaseInfo user = iUserService.getUserDetail(userid);
        user.setCompanyId(UserInfoContext.getUser().getCompanyId());
        return new ResultObject(user);
    }

    @PostMapping("/user/list")
    @ApiOperation(value = "查找用户列表", notes = "查找用户列表")
    public ResultObject listUser(@RequestBody UserFindBo userFindBo) {
        PageHelper.startPage(userFindBo.getPageNum(), userFindBo.getPageSize());
        List<UserBaseInfo> list = iUserService.listUser(userFindBo);
        PageInfo<UserBaseInfo> pageInfo = new PageInfo<>(list);
        return new ResultObject(pageInfo);
    }

    @PostMapping("/user/bindRole")
    @ApiOperation(value = "用户绑定角色", notes = "用户绑定角色")
    public ResultObject binduser(@RequestBody UserRoleBindBo userRoleBindBo) {
        iUserService.bindRole(userRoleBindBo);
        return new ResultObject();
    }

    @PostMapping("/user/generateSecretPassword")
    @ApiOperation(value = "密码校验", notes = "密码校验")
    @ApiImplicitParam(name = "secretBo", value = "密码对象", required = true, dataType = "SecretBo")
    public ResultObject<SecretBo> generateSecretPassword(@RequestBody SecretBo secretBo) {
        secretBo = iUserService.generateSecretPassword(secretBo.getPassword(), secretBo.getSalt());
        return new ResultObject(secretBo);
    }

    @PostMapping("/user/getCtfUserList")
    @ApiOperation(value = "通过userIdList查找攻防靶场用户列表", notes = "通过userIdList查找攻防靶场用户列表")
    public ResultObject getCtfUserList(@RequestBody List<Integer> userIdList) {
        List<UserBaseInfo> list = iUserService.getCtfUserList(userIdList);
        return new ResultObject(list);
    }


    @PostMapping("/user/approveList")
    @ApiOperation(value = "需要开通应用审核列表", notes = "需要开通应用审核列表")
    public ResultObject<PageInfo<UserBaseInfoBo>>   userApproveList(@RequestBody  PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        UserCore user = UserInfoContext.getUser();
        List<UserBaseInfoBo> list = iUserService.userApproveList(user);
        return  new ResultObject( new PageInfo<>(list));
    }

    @PostMapping("/user/approve")
    @ApiOperation(value = "审核用户是否开通应用", notes = "审核用户是否开通应用")
    public ResultObject userApprove(@RequestBody UserClientApproveBo userClientApprove) {
        UserCore user = UserInfoContext.getUser();
        userClientApprove.setClientId(user.getClientId());
        userClientApprove.setCompanyId(user.getCompanyId());
        iUserService.openUserClient(userClientApprove);
        return new ResultObject();
    }

    @PostMapping("/user/personal/list")
    @ApiOperation(value = "查找个人用户列表", notes = "查找个人用户列表且用户没有绑定此公司应用")
    public ResultObject listPersonalUser(@RequestBody UserFindBo userFindBo) {
        PageHelper.startPage(userFindBo.getPageNum(), userFindBo.getPageSize());
        UserCore user = UserInfoContext.getUser();
        userFindBo.setCompanyId(user.getCompanyId().toString());
        userFindBo.setClientId(user.getClientId());
        List<UserBaseInfo> list = iUserService.listPersonalUser(userFindBo);
        PageInfo<UserBaseInfo> pageInfo = new PageInfo<>(list);
        return new ResultObject(pageInfo);
    }

    @PostMapping("/user/bindApp")
    @ApiOperation(value = "个人用户绑定到应用", notes = "个人用户绑定到应用")
    public ResultObject userBindApp(@RequestBody UserBindAppBo userBindAppBo) {
        UserCore user = UserInfoContext.getUser();
        userBindAppBo.setClientId(user.getClientId());
        userBindAppBo.setCompanyId(user.getCompanyId());
        iUserService.bindApp(userBindAppBo);
        return new ResultObject();
    }

    @GetMapping("/user/phone")
    @ApiOperation(value = "手机号查询用户详情", notes = "手机号查询用户详情")
    public ResultObject userBindApp(@RequestParam String phone) {
        User userByPhone = iUserService.getUserByPhone(phone);
        return new ResultObject(userByPhone);
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "用户接口进行注册", notes = "用户接口进行注册(不是登录使用，主要是内部调用)")
    public ResultObject<InnerRegisterResp> userRegister(@RequestBody UserInsertBo userInsertBo) {
        InnerRegisterResp  innerRegisterResp = iUserService.userInnerRegister(userInsertBo);
        return new ResultObject(innerRegisterResp);
    }
    @PostMapping("/user/excel/import")
    @ApiOperation(value = "excel导入用户到应用列表", notes = "excel导入用户到应用列表")
    public ResultObject importExcelUser(@RequestParam ("file") MultipartFile file) {
        log.info("excel导入用户文件的名称：{}", file.getOriginalFilename());
        UserCore user = UserInfoContext.getUser();
        iUserService.importExcelUser(file,user);
        return new ResultObject();
    }


}
